package com.kalaqia.permission.aspect;

import android.content.ComponentName;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.provider.Settings;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * 功能：权限空值跳转工具
 * Created by kalaqia on 2017/6/8.
 * 邮箱：421407393@qq.com
 */
public class PermissionSettingsUtils {

    private static PermissionSettingsUtils instance;

    public static PermissionSettingsUtils getInstance() {
        if (instance == null) {
            instance = new PermissionSettingsUtils();
        }
        return instance;
    }

    private PermissionSettingsUtils() {

    }

    /**
     * 获取跳转权限空值的intent
     */
    public Intent getPermissionSettingIntent() {
        if (isMIUI()) {
            String miuiInfo = getSystemProperty();
            if (miuiInfo.contains("7") || miuiInfo.contains("6")) {
                return getMiui67PermissionIntent();
            } else {
                return getMiui8PermissionIntent();
            }
        }
        if (isSamsung()) return getSamSungPermissionIntent();
        if (isHuaWei()) return getSettingsIntent();
        if (isMeizu()) return getMeiZuPermissionIntent();
        if (isSony()) return getSonyPermissionIntent();
        if (isOppo()) return getOppoPermissionIntent();
        if (isLG()) return getLGPermissionIntent();
        if (isLetv()) return getLetvPermissionIntent();
        return getNormalPermissionIntent();
    }

    private static String getSystemProperty() {
        String line = "";
        BufferedReader input = null;
        try {
            Process p = Runtime.getRuntime().exec("getprop ro.miui.ui.version.name");
            input = new BufferedReader(new InputStreamReader(p.getInputStream()), 1024);
            line = input.readLine();
        } catch (IOException ex) {
            ex.printStackTrace();
        } finally {
            if (input != null) {
                try {
                    input.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return line;
    }

    private Intent getSettingsIntent() {
//        Intent mIntent = new Intent();
//        mIntent.setClassName("com.android.settings", "com.android.settings.Settings");
//        startActivity(intent);
//        return new Intent(Settings.ACTION_SETTINGS);
        Intent intent = new Intent();
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.putExtra("packageName", BuildConfig.APPLICATION_ID);
        ComponentName comp = new ComponentName("com.huawei.systemmanager", "com.huawei.permissionmanager.ui.MainActivity");
        intent.setComponent(comp);
        return intent;
    }

    public Intent getNormalPermissionIntent() {
        Intent intent = new Intent();
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
        intent.setData(Uri.fromParts("package", BuildConfig.APPLICATION_ID, null));
        return intent;
    }

    /**
     * 检查手机是否是miui
     */
    public boolean isMIUI() {
        return Build.MANUFACTURER.equalsIgnoreCase("Xiaomi");
    }

    public boolean isMeizu() {
        return Build.MANUFACTURER.equalsIgnoreCase("Meizu");
    }

    public boolean isSamsung() {
        return Build.MANUFACTURER.equalsIgnoreCase("samsung");
    }

    public boolean isHuaWei() {
        return Build.MANUFACTURER.equalsIgnoreCase("HUAWEI");
    }

    public boolean isSony() {
        return Build.MANUFACTURER.equalsIgnoreCase("Sony");
    }

    public boolean isOppo() {
        return Build.MANUFACTURER.equalsIgnoreCase("OPPO");
    }

    public boolean isLG() {
        return Build.MANUFACTURER.equalsIgnoreCase("LG");
    }

    public boolean isLetv() {
        return Build.MANUFACTURER.equalsIgnoreCase("Letv");
    }

    /**
     * MI MAX （MIUI 8.2）
     */
    public Intent getMiui8PermissionIntent() {
        Intent i = new Intent("miui.intent.action.APP_PERM_EDITOR");
        i.putExtra("extra_pkgname", BuildConfig.APPLICATION_ID);
        i.setClassName("com.miui.securitycenter", "com.miui.permcenter.permissions.PermissionsEditorActivity");
        return i;
    }

    /**
     *
     * @return
     */
    public Intent getMiui67PermissionIntent() {
        Intent i = new Intent("miui.intent.action.APP_PERM_EDITOR");
        i.putExtra("extra_pkgname", BuildConfig.APPLICATION_ID);
        i.setClassName("com.miui.securitycenter", "com.miui.permcenter.permissions.AppPermissionsEditorActivity");
        return i;
    }

    /**
     * samsung s6 5.1.1
     */
    public Intent getSamSungPermissionIntent() {
        Intent i = new Intent();
        i.setClassName("com.sec.android.app.capabilitymanager", "com.sec.android.app.cm.ui.CMApplicationListActivity");
        return i;
    }

    /**
     * 跳转到魅族的权限管理系统
     */
    public Intent getMeiZuPermissionIntent() {
        Intent intent = new Intent("com.meizu.safe.security.SHOW_APPSEC");
        intent.addCategory(Intent.CATEGORY_DEFAULT);
        intent.putExtra("packageName", BuildConfig.APPLICATION_ID);
        return intent;
    }

    public Intent getSonyPermissionIntent() {
        Intent intent = new Intent();
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.putExtra("packageName", BuildConfig.APPLICATION_ID);
        ComponentName comp = new ComponentName("com.sonymobile.cta", "com.sonymobile.cta.SomcCTAMainActivity");
        intent.setComponent(comp);
        return intent;
    }

    public Intent getOppoPermissionIntent() {
        Intent intent = new Intent();
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.putExtra("packageName", BuildConfig.APPLICATION_ID);
        ComponentName comp = new ComponentName("com.color.safecenter", "com.color.safecenter.permission.PermissionManagerActivity");
        intent.setComponent(comp);
        return intent;
    }

    public Intent getLGPermissionIntent() {
        Intent intent = new Intent("android.intent.action.MAIN");
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.putExtra("packageName", BuildConfig.APPLICATION_ID);
        ComponentName comp = new ComponentName("com.android.settings", "com.android.settings.Settings$AccessLockSummaryActivity");
        intent.setComponent(comp);
        return intent;
    }

    public Intent getLetvPermissionIntent() {
        Intent intent = new Intent();
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.putExtra("packageName", BuildConfig.APPLICATION_ID);
        ComponentName comp = new ComponentName("com.letv.android.letvsafe", "com.letv.android.letvsafe.PermissionAndApps");
        intent.setComponent(comp);
        return intent;
    }

    /**
     * 跳转华为的权限管理系统
     */
    public Intent getHuaWeiPermissionIntent() {
        Intent intent = new Intent("huawei.intent.action.HSM_PERMISSION_MANAGER");
        ComponentName comp = new ComponentName("com.huawei.systemmanager", "com.huawei.permissionmanager.ui.MainActivity");//华为权限管理
        intent.setComponent(comp);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        return intent;
    }

}
